package com.reader.cms.controller.magazine;

import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.magazine.ReaderMagazine;
import com.reader.cms.entity.magazine.ReaderMagazineContent;
import com.reader.cms.service.magazine.IReaderMagazineContentService;
import com.reader.core.entity.ResultMessageInfo;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 杂志内容有关接口
 * @date 2018-10-24
 */
@RestController
@RequestMapping("/api/readermagazinecontent")
public class ReaderMagazineContentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderMagazineContentController.class);

    @Autowired
    public IReaderMagazineContentService iReaderMagazineContentService;


    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param magazineId  杂志ID
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "magazineId", required = true) String magazineId,
                                            @RequestParam(value = "title",required = false)String title) {
        ResultMessageInfo resultMessage = null;
        Page<ReaderMagazineContent> list = iReaderMagazineContentService.selectListPage(currentPage, size, magazineId,title);
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerMagazineContent 实体对象
     * @return
     * @function 添加新单期杂志文章
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderMagazineContent readerMagazineContent) {
        ValidatorUtil.validateEntity(readerMagazineContent, AddGroup.class);//非空验证
        String id = UUID.randomUUID().toString().replace("-", "");
        readerMagazineContent.setId(id);
        readerMagazineContent.setPublishDt(new Date());
        Integer userId = this.getUserId();
        readerMagazineContent.setInsertDt(new Date());
        readerMagazineContent.setEnable(1);
        readerMagazineContent.setDelFlag(0);
        readerMagazineContent.setInsertUserId(userId);
        readerMagazineContent.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineContentService.addMagazineContent(readerMagazineContent);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }


    /**
     * @return
     * @function 获取单篇杂志文章的编辑回显数据
     */
    @GetMapping("update_view/{id}")
    public ResultMessageInfo getEditMagazine(@PathVariable(value = "id") String id) {
        ReaderMagazineContent magazineContent = iReaderMagazineContentService.selectSingleContent(id);
        if (magazineContent == null) {
            return ResultMessageInfo.ofMessage("文章数据错误，请联系管理员！！！");
        }
        return ResultMessageInfo.ofSuccess("查询成功！", magazineContent);
    }

    /**
     * @param readerMagazineContent 实体对象
     * @return
     * @function 更新单期杂志文章内容
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value = "跟据ID列新记录", notes = "id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody ReaderMagazineContent readerMagazineContent) {
        ValidatorUtil.validateEntity(readerMagazineContent, AddGroup.class);//非空验证
        Integer userId = this.getUserId();
        readerMagazineContent.setUpdtDt(new Date());
        readerMagazineContent.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineContentService.updateMagazineContent(readerMagazineContent);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");

        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @return
     * @function 删除单期杂志文章内容
     */
    @PostMapping("delete")
    @Transactional
    public ResultMessageInfo deleteById(@RequestBody ReaderMagazineContent readerMagazineContent) {
        Integer userId = this.getUserId();
        readerMagazineContent.setDelFlag(1);
        readerMagazineContent.setUpdtDt(new Date());
        readerMagazineContent.setUpdtUserId(userId);
        boolean resultValue = iReaderMagazineContentService.updateById(readerMagazineContent);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除杂志文章
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody List<ReaderMagazineContent> readerMagazineContents) {
        Integer userId = this.getUserId();
        readerMagazineContents.forEach(readerMagazineContent -> {
            readerMagazineContent.setDelFlag(1);
            readerMagazineContent.setUpdtDt(new Date());
            readerMagazineContent.setUpdtUserId(userId);
        });
        boolean resultValue = iReaderMagazineContentService.updateBatchById(readerMagazineContents);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}